package com.project.service.impl;

import com.project.bean.*;
import com.project.dao.ICheckCarDao;
import com.project.dao.ICheckDao;
import com.project.dao.ICheckInfoDao;
import com.project.dao.IProductDao;
import com.project.service.ICheckCarService;
import com.project.util.InfoResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * @author lyl
 * description:预出库管理
 */
@Service
@Transactional
public class CheckCarServiceImpl implements ICheckCarService {
    @Autowired
    private ICheckCarDao iCheckCarDao;
    @Autowired
    private ICheckDao iCheckDao;
    @Autowired
    private ICheckInfoDao iCheckInfoDao;
    @Autowired
    private IProductDao iProductDao;

    @Override
    public InfoResult checkOut(int userId, int clientId, String address) {
        List<CheckCarBean> list = iCheckCarDao.showCheckCarById(userId, clientId);
        if (list == null) {
            return new InfoResult(1, "出库失败,无可用出库信息");
        }
        //添加新订单信息
        CheckBean checkBean = new CheckBean();
        ClientBean cl = new ClientBean();
        cl.setClientId(clientId);
        UserBean us = new UserBean();
        us.setUserId(userId);
        checkBean.setClientBean(cl);
        checkBean.setUserBean(us);
        checkBean.setAddress(address);
        checkBean.setSaleDate(LocalDate.now());
        iCheckDao.addNewCheck(checkBean);

        //循环进行添加新的出库订单详情信息
        for (int i = 0; i < list.size(); i++) {
            CheckCarBean c = list.get(i);
            iCheckInfoDao.addCheckInfo(c.getProductBean().getProductId(), c.getCheckNum(), checkBean.getCheckId(), c.getProductBean().getSalePrice());
            //更新库存商品的数量
            ProductBean p = iProductDao.selectById(c.getProductBean().getProductId());
            p.setProductNum((int) (p.getProductNum() - c.getCheckNum()));
            iProductDao.updateById(p);
        }
        //清空
        iCheckCarDao.checkOut(userId, clientId);
        return new InfoResult(0, "添加订单成功");
    }

    @Override
    public InfoResult addCheckToCar(int userId, int productId, int clientId) {
        //检测添加的商品是否已经存在于订单中
        CheckCarBean c = iCheckCarDao.checkNumExist(userId, productId, clientId);
        if (c != null) {
            //已经存在，直接更新数量
            return updateCheckCarInfo(userId, productId, (int) (c.getCheckNum() + 1));
        }
        //不存在的商品
        int x = iCheckCarDao.addCheckToCar(userId, productId, clientId);
        if (x == 1) {
            return new InfoResult(0, "添加新商品成功");
        }
        return new InfoResult(1, "添加新商品失败");
    }

    @Override
    public List<CheckCarBean> showCheckCarById(int userId, int clientId) {
        return iCheckCarDao.showCheckCarById(userId, clientId);
    }

    @Override
    public InfoResult updateCheckCarInfo(int userId, int productId, int checkNum) {
        int x = iCheckCarDao.updateCheckCarInfo(userId, productId, checkNum);
        if (x == 1) {
            return new InfoResult(0, "更新数量成功");
        }
        return new InfoResult(1, "更新数量失败");
    }

    @Override
    public InfoResult remove(int checkCarId) {
        int x = iCheckCarDao.deleteById(checkCarId);
        if (x == 1) {
            return new InfoResult(0, "更新数量成功");
        }
        return new InfoResult(1, "更新数量失败");
    }
}
